<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class App_model extends CI_Model {

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
                $this->load->database();
	}

	function list_app_user($userid){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$this->db->where('user_id',$userid);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function list_app_by_cate_id($cateid=null){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$this->db->where('appcate',$cateid);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	
	function list_app_by_search($keyword=null,$userid=null){
		$this->db->order_by('id', 'DESC');
		$sql = "select * from tbl_app where app_name like '%" . $keyword . "%' and user_id =".$userid;
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return null;
		}
	}
	
	function return_app_name($app_id){
		$this->load->database(); 
		$this->db->where('id',$app_id);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $row){
				return $row->app_name;
			}
		}
		return $query->result();
	}
	
	function return_app_cate($app_id){
		$this->load->database(); 
		$this->db->where('id',$app_id);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $row){
				return $row->catewap;
			}
		}
		return $query->result();
	}
	
	function get_app_by_cate($cateid,$userid,$currentapp){
		$this->load->database(); 
		$this->db->limit(3);
		$this->db->order_by("viewcount", "desc");
		$this->db->where('catewap',$cateid);
		$this->db->where('user_id',$userid);
		$this->db->where('id !=', $currentapp);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	
	   
	function get_app($userid,$id){
		$this->load->database();
		$this->db->where('user_id',$userid);
		$this->db->where('id',$id);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

 
	
	function check_app_name($appname){
		$this->db->where('app_name',strtolower($appname));
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			return 1;
		}else{
			return 0;
		}
	}

	public function add_app($userid,$appcate,$appicon,$appname,$applink,$appdes,$active,$shortcode,$message,$timeactive,$apptype){
	 
		if($this->check_app_name($appname) == 0){
			$data = array(
					'user_id'=>$userid,
					'appcate'=>$appcate,
					'app_name'=>strtolower($appname),
					'app_link'=>$applink,
					'appdes'=>$appdes,
					'datecreated'=>date("Y-m-d h:s:m"),
					'active'=>$active,
					'shortcode'=>$shortcode,
					'message'=>$message,
					'timeactive'=>$timeactive,
					'apptype'=>$apptype,
					'appicon' =>$appicon,
			);
			$this->db->insert('tbl_app', $data);
			return 1;
		} else {
			return 0;
		}
	}

	function update_app($id,$userid,$appcate,$appicon=null,$appname,$applink,$appdes,$active,$shortcode,$message,$timeactive,$apptype){
		$this->load->database();
		$data = array(
				'user_id'=>$userid,
				'appcate'=>$appcate,
				'app_name'=>strtolower($appname),
				'app_link'=>$applink,
				'active'=>$active,
				'dateupdated'=>date("Y-m-d h:s:m"),
		);
		$this->db->where('id', $appid);
		$this->db->update('tbl_app', $data);
	}
	
	public function update_viewcount($appid){
		$this->load->database();
		$this->db->where('id', $appid);
		$count = 0;
		$query = $this->db->get('tbl_app');
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $rows) {
				$count = $rows->viewcount;
			}
		}
		$this->db->query('UPDATE tbl_app SET viewcount=' . ($count + 1) . ' WHERE id=' . $appid);
	}
	
	function add_image_for_item($userid,$itemid, $item_image) {
	
		$this->load->database();
		$data = array(
				'userid'=>$userid,
				'itemid' => $itemid,
				'imageslink' => $item_image,
				'datecreated' => date("d-m-Y H:i:s"),
				'active' => 1
		);
		$this->db->insert('tbl_gallery_item', $data);
	}
	
	function show_allgallery($userid ){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$this->db->where('userid',$userid); 
		$query = $this->db->get('tbl_gallery_item');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		return $query->result();
	}
	
		function show_allgallery_details($userid,$itemid ){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$this->db->where('userid',$userid); 
		$this->db->where('itemid',$itemid);
		$query = $this->db->get('tbl_gallery_item');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		return $query->result();
	}
	 
	
	function show_image_by_appid($userid){
		$this->load->database();
		$this->db->where('itemid',$userid);
		$this->db->order_by("id", "desc");
		$query = $this->db->get('tbl_gallery_item');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		return $query->result();
	}
	
	function del_image_gallery($id,$userid) {
		$this->load->database();
		$this->db->where('id', $id);
		$this->db->where('userid', $userid);
		$this->db->delete('tbl_gallery_item');
	}

	function del_app($userid,$appid){
		$this->load->database();
		$this->db->where('user_id', $userid);
		$this->db->where('id', $appid);
		$this->db->delete('tbl_app');
	}

}
